#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from .default import DefaultConfig


class ProductionConfig(DefaultConfig):
    """生产环境配置"""
    
    # 生产环境必须设置SECRET_KEY环境变量
    SECRET_KEY = os.environ.get("SECRET_KEY")
    
    # 生产环境CORS配置
    CORS_ORIGINS = [
        os.environ.get("FRONTEND_URL", "https://example.com")
    ]
    
    # 生产环境日志配置
    LOG_LEVEL = "WARNING"
    
    # 生产环境安全配置
    SESSION_COOKIE_SECURE = True
    REMEMBER_COOKIE_SECURE = True
    SESSION_COOKIE_HTTPONLY = True
    REMEMBER_COOKIE_HTTPONLY = True
    
    # 请求限流配置
    RATELIMIT_DEFAULT = "100 per day, 20 per hour"
    RATELIMIT_STORAGE_URL = os.environ.get("REDIS_URL", "memory://")
    
    @classmethod
    def init_app(cls, app):
        """生产环境特定的应用初始化"""
        # 确保SECRET_KEY已设置
        assert cls.SECRET_KEY, "生产环境必须设置SECRET_KEY环境变量"
        
        # 配置生产环境日志
        import logging
        from logging.handlers import RotatingFileHandler
        
        file_handler = RotatingFileHandler(
            "logs/app.log", maxBytes=10*1024*1024, backupCount=10
        )
        file_handler.setFormatter(logging.Formatter(cls.LOG_FORMAT))
        file_handler.setLevel(getattr(logging, cls.LOG_LEVEL))
        
        app.logger.addHandler(file_handler) 